package com.fzkj;

/**
 * 统计对象的实例数量
 */
public class User {
    String name;
    int age;
    private static int count;

    private synchronized void add(){
        count++;
    }

    public User(String name, int age) {
        add();
        this.name = name;
        this.age = age;
    }

    public static int getCount(){
        return count;
    }

    @Override
    protected void finalize() throws Throwable {
        count--;
        super.finalize();
    }
}

class Man {
    private static int count;

    public Man() {
        count++;
    }

    public static int getCount(){
        return count;
    }
}

class Main{
    public static void main(String[] args) {
        User user = new User("1", 1);
        User user2 = new User("12", 12);
        User user3 = new User("13", 13);
        User user4 = new User("14", 14);

        System.out.println(User.getCount());
    }
}
